#include <iostream>
using namespace std;

#include "glad/glad.h"
#include "GLFW/glfw3.h"
#include "glm_use.h"

#include "constant.h"
#include "Playing.h"
#include "Shape.h"
#include "callbacks.h"
void processinput(GLFWwindow *window);
int main()
{
    //初始化GLFW
    glfwInit();

    //设置版本号
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

#ifdef __APPLE__
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif

    //创建窗口对象
    GLFWwindow *window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "3D打砖块", NULL, NULL);
    if (window == NULL)
    {
        cout << "Failed to create GLFW window" << endl;
        glfwTerminate();
        exit(-1);
    }
    glfwMakeContextCurrent(window);

    //初始化GLAD
    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
    {
        cout << "Failed to initialize GLAD" << endl;
        exit(-1);
    }
    glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
    glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);

    shapeInit();

    Playing *scene = new Playing(window);
    S_Playing::init(scene);
    S_Playing::setcallbacks();
    scene->init();

    //主循环
    float lastTime = glfwGetTime();
    while (!glfwWindowShouldClose(window))
    {
        float currentTime = glfwGetTime();
        float dt = currentTime - lastTime;
        lastTime = currentTime;
        processinput(window);
        scene->draw();
        scene->update(dt);

        glfwSwapBuffers(window);
        glfwPollEvents();
    }
    scene->deinit();
    glfwTerminate();
    return 0;
}
void processinput(GLFWwindow *window)
{
    if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
        glfwSetWindowShouldClose(window, 1);
}